#!/bin/bash
#
#   xrkmonitor license 
#
#   Copyright (c) 2020 by rockdeng
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
#
#   字符云监控(xrkmonitor) 开源版 (c) 2021 by rockdeng
#   当前版本：v1.0
#   使用授权协议： apache license 2.0
#
#   开源版主页：http://open.xrkmonitor.com
#   云版本主页：http://xrkmonitor.com
#  
#
#   云版本为开源版提供永久免费告警通道支持，告警通道支持短信、邮件、
#   微信等多种方式，欢迎使用
#
#   监控插件 mysql_slave_check 功能:
#        mysql 主从同步监控，主从不同步时会记录错误日志并发送异常告警
#
#   插件开发时间：2021-11-19
#   插件开发人员：官方
#   插件开发人员云账号ID：1
#
#   脚本功能：
#       执行插件逻辑，该脚本在插件安装完成后由 linux crontab 定时执行
#

# 进入脚本所在目录
script_path=''
script_name=''
function in_script_path()
{
	echo "/" > _tmp
	fc=`expr substr "$0" 1 1`
	echo "$fc" > _tmp_2
	cmp _tmp _tmp_2 > /dev/null 2>&1
	if [ $? -eq 0 ];then
        cscript=$0
	else
        cscript=$(pwd)/$0
	fi
	rm -f _tmp _tmp_2
	script_path=`dirname "$cscript"`
	script_name=`basename "$cscript"`
	cd "$script_path"
	script_path=`pwd`
}
in_script_path

# check 下是否手动 stop 的
if [ -f _manual_stop_ ] ; then 
	exit 0;
fi

xrk_plugin_file_sh=./xrk_mysql_slave_check.sh
xrk_plugin_file_conf=${script_path}/xrk_mysql_slave_check.conf

# shell 上报工具使用版本号比较大的
xrkmonitor_bin=${script_path}/xrkmonitor_report
if [ ! -x $xrkmonitor_bin ]; then
    if [ ! -x /usr/bin/xrkmonitor_report ]; then
        echo "not find execute file:xrkmonitor_report"
        exit 2
    else
        cp /usr/bin/xrkmonitor_report ${xrkmonitor_bin}
    fi
elif [ ! -x /usr/bin/xrkmonitor_report ]; then
    cp $xrkmonitor_bin /usr/bin > /dev/null 2>&1
else
    tool_v1=`/usr/bin/xrkmonitor_report version`
    tool_v2=`${xrkmonitor_bin} version`
    if [[ "$tool_v1" > "$tool_v2" ]]; then
        cp /usr/bin/xrkmonitor_report ${xrkmonitor_bin} -f
    elif [[ "$tool_v1" < "$tool_v2" ]]; then
        cp ${xrkmonitor_bin} /usr/bin -f > /dev/null 2>&1
    fi
fi

if [ ! -f $xrk_plugin_file_sh ] ; then 
    echo "not find file:$xrk_plugin_file_sh"
    exit 1
fi

if [ ! -f $xrk_plugin_file_conf ]; then
	echo "not find xrkmonitor config file:$xrk_plugin_file_conf"
	exit $LINENO
fi

# 确保同一时间只运行一个脚本实例
function check_process_run()
{
	pname="$1"
	if [ -f "${pname}.pid" ]; then
		oldpid=`cat ${pname}.pid`
		pgrep -f ${pname}|grep $oldpid > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			echo "already run, pid:$oldpid !"
			dinfo=`date`
			if [ -f "check_process-${pname}.log" ]; then
				clogsize=`stat --format %s check_process-${pname}.log`
				if [ $clogsize -gt 1048576 ]; then
					rm "check_process-${pname}.log"
					touch "check_process-${pname}.log"
				fi
			fi
			echo "$dinfo: already run, pid:$oldpid ! - $$ -" >> "check_process-${pname}.log"
			exit $LINENO
		fi
	fi
	rm -f "${pname}.pid" 
	touch "${pname}.pid"
	echo $$ > "${pname}.pid"
}

function check_process_atexit()
{
    cd "$script_path"
    if [ -f "${script_name}.pid" ]; then
        rm -f "${script_name}.pid"
    fi  
}
check_process_run "$script_name" 

# 获取配置
# 参数1 - 配置项的宏名
function xrkmonitor_cfg()
{
	pcfgname="$1"
    cfg_val_src=`grep "^${pcfgname} " $xrk_plugin_file_conf`
    if [ $? -ne 0 ]; then
        echo ""
        return 1
    fi
    cfg_val=`echo $cfg_val_src|awk '{print $2}'|sed 's/^M//g'`
    echo $cfg_val
    return 0
}

# 累加上报监控点
# 参数1 - 监控点宏名
# 参数2 - 上报值
function xrkmonitor_add_attr()
{
    attr=$1
    num=$2 
    ${xrkmonitor_bin} file "$xrk_plugin_file_conf" "attr" "add" "$attr" "$num"
}

# 设置上报监控点
# 参数1 - 监控点宏名
# 参数2 - 上报值
function xrkmonitor_set_attr()
{
    attr=$1
    num=$2 
    ${xrkmonitor_bin} file "$xrk_plugin_file_conf" "attr" "set" "$attr" "$num" 
}

# 累加上报字符串监控点
# 参数1 - 字符串监控点宏名
# 参数2 - 字符串
# 参数3 - 上报值
function xrkmonitor_add_strattr()
{
    attr=$1
    str=$2
    num=$3 
    ${xrkmonitor_bin} file "$xrk_plugin_file_conf" "strattr" "add" "$attr" "$str" "$num"
}

# 设置上报字符串监控点
# 参数1 - 字符串监控点宏名
# 参数2 - 字符串
# 参数3 - 上报值
function xrkmonitor_set_strattr()
{
    attr=$1
    str=$2
    num=$3 
    ${xrkmonitor_bin} file "$xrk_plugin_file_conf" "strattr" "set" "$attr" "$str" "$num"
}

# 上报日志
# 参数1 - 日志类型 (debug,info,warn,reqerr,error,fatal)
# 参数2 - 日志内容
function xrkmonitor_log()
{
    logtype=$1
    logcontent=$2 
    ${xrkmonitor_bin} file "$xrk_plugin_file_conf" "log" "$logtype" "$logcontent"
}

# 上报实时表格
# 参数1 - 表格id 宏名
# 参数2 - 脚本统计时间间隔
# 参数3 - 表格状态id，在单机多条记录时相同的状态id的记录将会一起展示出来
# 参数4 - 实时表格内容，必须是按一定格式上报，字段的先后顺序不重要
function xrkmonitor_table_log()
{
    table_id_str=$1
    static_time=$2
    stat_id=$3
    logcontent=$4 
    ${xrkmonitor_bin} file "$xrk_plugin_file_conf" "table" "$table_id_str" "$static_time" "$stat_id" "$logcontent"
}

# 请勿删除以下代码，确保脚本运行时有数据上报或者接口调用
xrkmonitor_log "info" "start run linux shell plugin: mysql_slave_check"

# 调用插件脚本
. $xrk_plugin_file_sh 

check_process_atexit
exit 0

